/*
 * Link.cpp
 *
 *  Created on: 14.04.2010
 *      Author: king
 */

#include <QtGui>
#include "Link.h"
#include "Node.h"

namespace etu {
namespace ui {

static const int RADIUS = 10;
//static const double M_PI = 3.14159;

CLink::CLink(CNode *fromNode, CNode *toNode)
{
    m_fromNode = fromNode;
    m_toNode = toNode;

    m_fromNode->addLink(this);
    m_toNode->addLink(this);

    //setFlags(QGraphicsItem::ItemIsSelectable);
    setZValue(-1);

    setColor(Qt::darkRed);
    trackNodes();
}

CLink::~CLink()
{
    m_fromNode->removeLink(this);
    m_toNode->removeLink(this);
}

CNode *CLink::fromNode() const
{
    return m_fromNode;
}

CNode *CLink::toNode() const
{
    return m_toNode;
}

void CLink::setColor(const QColor &color)
{
    setPen(QPen(color, 1.0));
}

QColor CLink::color() const
{
    return pen().color();
}

void CLink::trackNodes()
{
    setLine(QLineF(m_fromNode->scenePos(), m_toNode->scenePos()));
    update();
}

void CLink::paint(QPainter *painter,
                 const QStyleOptionGraphicsItem *option,
                 QWidget * /* widget */)
{
/*
	QVector<QPoint> pointVector;

	for (size_t i=0; i<3; ++i)
	{
	  int polygonX = int(RADIUS*sin(i*(2*M_PI/3)))+m_toNode->pos().x();
	  int polygonY = -int(RADIUS*cos(i*(2*M_PI/3)))+m_toNode->pos().y() + m_toNode->boundingRect().height() ;
	  QPoint point(polygonX, polygonY);

	  pointVector.push_back(point);
	}

	QPolygon polygon(pointVector);
	Qt::FillRule fillRule;
	painter->drawPolygon(polygon,fillRule);*/

	QGraphicsLineItem::paint(painter,option);
}

}
}
